package com.wll.ec.core.wechat;

import android.app.Activity;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wll.ec.core.app.ConfigType;
import com.wll.ec.core.app.Ec;
import com.wll.ec.core.wechat.callbacks.IWeChatSignInCallback;

/**
 * created by detachment on 2020/3/20
 */
public class EcWeChat {

    public static final String APP_ID = Ec.getConfiguration(ConfigType.WE_CHAT_APP_ID);
    public static final String APP_SECRET = Ec.getConfiguration(ConfigType.WE_CHAT_APP_SECRET);
    private final IWXAPI WXAPI;
    private IWeChatSignInCallback mSignInCallback = null;

    private static final class Holder{
        private static final EcWeChat INSTANCE = new EcWeChat();
    }

    public static EcWeChat getInstance(){
        return Holder.INSTANCE;
    }

    private EcWeChat(){
        final Activity activity = Ec.getConfiguration(ConfigType.WE_CHAT_ACTIVITY);
        WXAPI = WXAPIFactory.createWXAPI(activity, APP_ID, true);
        WXAPI.registerApp(APP_ID);
    }

    public final IWXAPI getWXAPI(){
        return WXAPI;
    }

    public EcWeChat setSignInCallback(IWeChatSignInCallback callback){
        this.mSignInCallback = callback;
        return this;
    }

    public IWeChatSignInCallback getSignInCallback(){
        return mSignInCallback;
    }

    public final void signIn(){
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "random_state";
        WXAPI.sendReq(req);
    }

}
